#include "oled12864.h"
#include <math.h>


OLED_12864::OLED_12864()
{
    brushWidth = 1;
    brushColor = 1;
}

OLED_12864::~OLED_12864()
{
    
}

bool OLED_12864::begin()
{
    // 初始化OLED的基本配置
    Wire.begin();
    SSD1306_I2CBEGIN();
    
    // 发送一些基本的初始化命令（根据SSD1306的标准初始化序列）
    Wire.beginTransmission(SSD1306_ADDR);
    Wire.write(0x00); // 命令模式
    Wire.write(0xAE); // 关闭显示
    Wire.write(0xD5); // 设置显示时钟分频比/振荡器频率
    Wire.write(0x80); // 推荐值
    Wire.write(0xA8); // 设置多路复用率
    Wire.write(0x3F); // 1/64 占空比
    Wire.write(0xD3); // 设置显示偏移
    Wire.write(0x00); // 不偏移
    Wire.write(0x40); // 设置显示起始行
    Wire.write(0x8D); // 电荷泵设置
    Wire.write(0x14); // 启用电荷泵
    Wire.write(0x20); // 设置内存寻址模式
    Wire.write(0x00); // 水平寻址模式
    Wire.write(0xA1); // 设置段重映射
    Wire.write(0xC8); // 设置COM输出扫描方向
    Wire.write(0xDA); // 设置COM引脚硬件配置
    Wire.write(0x12); // 序列配置
    Wire.write(0x81); // 设置对比度
    Wire.write(0xCF); // 对比度值
    Wire.write(0xD9); // 设置预充电周期
    Wire.write(0xF1); // 预充电周期值
    Wire.write(0xDB); // 设置VCOMH取消选择级别
    Wire.write(0x40); // VCOMH级别
    Wire.write(0xA4); // 显示整个显示缓冲区
    Wire.write(0xA6); // 正常显示（非反相）
    Wire.write(0xAF); // 开启显示
    Wire.endTransmission();
    
    clear();
    return true;
}

void OLED_12864::clear()
{
    // 清除整个显示
    for (int page = 0; page < 8; page++) {
        Wire.beginTransmission(SSD1306_ADDR);
        Wire.write(0x00); // 命令模式
        Wire.write(0xB0 + page); // 设置页地址
        Wire.write(0x00); // 设置低列地址
        Wire.write(0x10); // 设置高列地址
        Wire.endTransmission();
        
        Wire.beginTransmission(SSD1306_ADDR);
        Wire.write(0x40); // 数据模式
        for (int col = 0; col < 128; col++) {
            Wire.write(0x00); // 写入0以清除
        }
        Wire.endTransmission();
    }
}

// 可参数化的圆形绘制函数
void OLED_12864::drawCircle(int centerX, int centerY, int radius, bool fill) {
    // 边界检查
    if(centerX - radius < 0 || centerX + radius >= 128 || 
       centerY - radius < 0 || centerY + radius >= 64) {
        return;
    }
    
    if(fill) {
        // 填充圆形 - 使用扫描法
        for(int y = -radius; y <= radius; y++) {
            for(int x = -radius; x <= radius; x++) {
                // 使用距离公式检查点是否在圆内
                if(x*x + y*y <= radius*radius) {
                    rect(centerX + x, centerY + y, 1, 1, true);
                }
            }
        }
    } else {
        // 绘制圆边框 - 使用Bresenham算法
        int x = radius;
        int y = 0;
        int err = 0;
        
        while (x >= y) {
            // 绘制八个对称点
            rect(centerX + x, centerY + y, 1, 1, true);
            rect(centerX + y, centerY + x, 1, 1, true);
            rect(centerX - y, centerY + x, 1, 1, true);
            rect(centerX - x, centerY + y, 1, 1, true);
            rect(centerX - x, centerY - y, 1, 1, true);
            rect(centerX - y, centerY - x, 1, 1, true);
            rect(centerX + y, centerY - x, 1, 1, true);
            rect(centerX + x, centerY - y, 1, 1, true);
            
            y += 1;
            err += 1 + 2*y;
            if (2*(err - x) + 1 > 0) {
                x -= 1;
                err += 1 - 2*x;
            }
        }
    }
}

void OLED_12864::setBrushColor(int16_t color)
{
    brushColor = color;
}

void OLED_12864::regionalRefresh(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
{
    // 这里简单实现，实际可能需要更复杂的刷新逻辑
    // 由于我们直接使用rect绘制像素，这里可以留空
}

void OLED_12864::lineWidth(uint32_t w)
{
    brushWidth = w;
}

void OLED_12864::HLineOnePixel(int32_t x, int32_t y, int32_t width)
{
    // 绘制水平像素线
    if (x < 0 || y < 0 || x + width > 128 || y > 63) return;
    
    int page = y / 8;
    int bit = y % 8;
    uint8_t data = 1 << bit;
    
    // 设置页面和起始列地址
    Wire.beginTransmission(SSD1306_ADDR);
    Wire.write(0x00); // 命令模式
    Wire.write(0xB0 + page); // 设置页地址
    Wire.write(x & 0x0F); // 低列地址
    Wire.write(0x10 | (x >> 4)); // 高列地址
    Wire.endTransmission();
    
    // 一次传输多个像素数据，减少I2C传输次数
    Wire.beginTransmission(SSD1306_ADDR);
    Wire.write(0x40); // 数据模式
    
    // 对于水平线段，每个像素对应一个完整的字节（0xFF表示亮，0x00表示暗）
    for (int i = 0; i < width; i++) {
        Wire.write(brushColor ? 0xFF : 0x00);
    }
    
    Wire.endTransmission();
}

void OLED_12864::VLineOnePixel(int32_t x, int32_t y, int32_t height)
{
    // 绘制垂直像素线
    if (x < 0 || y < 0 || x > 127 || y + height > 64) return;
    
    // 优化垂直绘制，减少I2C传输次数
    // 按页面分组处理，每个页面内的点一起传输
    for (int page = 0; page < 8; page++) {
        // 计算此页面内需要绘制的位
        uint8_t pageData = 0;
        bool hasPixels = false;
        
        for (int i = 0; i < height; i++) {
            int currentY = y + i;
            if (currentY / 8 == page) {
                int bit = currentY % 8;
                pageData |= (1 << bit);
                hasPixels = true;
            }
        }
        
        if (hasPixels) {
            Wire.beginTransmission(SSD1306_ADDR);
            Wire.write(0x00); // 命令模式
            Wire.write(0xB0 + page); // 设置页地址
            Wire.write(x & 0x0F); // 低列地址
            Wire.write(0x10 | (x >> 4)); // 高列地址
            Wire.endTransmission();
            
            Wire.beginTransmission(SSD1306_ADDR);
            Wire.write(0x40); // 数据模式
            Wire.write(brushColor ? pageData : 0x00);
            Wire.endTransmission();
        }
    }
}

void OLED_12864::qrcode(uint32_t x, uint32_t y, const String& str, uint8_t scale)
{
    QRCode qrcode;
    uint8_t qrcodeData[qrcode_getBufferSize(3)];
    uint8_t ex, ey;
    uint8_t _brushWidth = brushWidth;
    int16_t _brushColor = brushColor;
    qrcode_initText(&qrcode, qrcodeData, 3, 0, str.c_str());
    lineWidth(1);
    for (uint8_t _y = 0; _y < qrcode.size; _y++) {
        // Each horizontal module
        for (uint8_t _x = 0; _x < qrcode.size; _x++) {
            if(qrcode_getModule(&qrcode, _x, _y))
                setBrushColor(1);
            else
                setBrushColor(0);
            rect(_x*scale + x ,_y*scale + y, scale, scale, true);
            ex = _x*scale + x + scale;
            ey = _y*scale + y + scale;
        }
        Serial.print("\n");
    }
    regionalRefresh(x, y, ex-x+1, ey-y+1);
    setBrushColor(_brushColor);
    lineWidth(_brushWidth);
}

void OLED_12864::setLineWidth(uint32_t w)
{
    lineWidth(w);
}

uint32_t OLED_12864::getLineWidth()
{
    return brushWidth;
}

void OLED_12864::showBuiltinImage(uint32_t x, uint32_t y, uint8_t imgType, uint16_t width, uint16_t height)
{
    int16_t _brushColor = brushColor;
    setBrushColor(1); // 确保画笔颜色为白色(显示)
    
    // 计算中心点
    uint16_t centerX = x + width / 2;
    uint16_t centerY = y + height / 2;
    
    // 根据图片类型绘制内置图片
    switch(imgType) {
        case 0: // HEART - 心形
            // 使用高精度心形绘制算法，确保心形更加饱满自然
            for(int pixelY = 0; pixelY < height; pixelY++) {
                for(int pixelX = 0; pixelX < width; pixelX++) {
                    // 计算归一化坐标
                    float normalizedX = (float)(pixelX - width/2) / (width/2);
                    float normalizedY = (float)(pixelY - height/2) / (height/2) * 1.25; // 增加Y轴比例，使心形更丰满
                    
                    // 使用改进的心形公式，参数调整使心形更圆润
                    float x2 = normalizedX * normalizedX;
                    float y2 = normalizedY * normalizedY;
                    float term1 = pow(x2 + y2 - 0.95f, 3.0f);
                    float term2 = 0.8f * x2 * pow(normalizedY, 3.0f);
                    
                    // 使用稍微宽松的阈值，使心形边缘更平滑
                    if(term1 - term2 <= 0.05f) {
                        // 直接绘制单个像素点
                        rect(x + pixelX, y + pixelY, 1, 1, true);
                    }
                }
            }
            break;
            
        case 1: // HAPPY - 笑脸
            // 使用更圆润的圆形脸，增加轻微的下巴
            drawCircle(centerX, centerY, width/2 * 0.9, true);
            // 添加圆润的下巴
            rect(x + width/4, y + height*0.85, width/2, height/12, true);
            
            // 绘制更生动的眼睛（圆形，带高光）
            setBrushColor(0);
            // 左眼 - 使用稍大的圆形
            drawCircle(x + width/3, y + height/4, width/15, true);
            // 右眼 - 使用稍大的圆形
            drawCircle(x + width*2/3, y + height/4, width/15, true);
            // 添加白色高光
            setBrushColor(1);
            drawCircle(x + width/3 - width/40, y + height/4 - width/40, width/40, true);
            drawCircle(x + width*2/3 - width/40, y + height/4 - width/40, width/40, true);
            setBrushColor(0);
            
            // 绘制大笑的嘴巴（更自然的微笑曲线）
            for(int scanY = 0; scanY < height/4; scanY++) {
                float normalizedY = (float)scanY / (height/4);
                // 使用更自然的余弦曲线计算嘴巴宽度
                float mouthWidth = width*3/5 * (0.6 + 0.4 * cos(normalizedY * PI));
                int mouthX = x + width/2 - mouthWidth/2;
                int mouthY = y + height*3/5 - scanY * 1.5; // 调整位置使笑容更灿烂
                
                // 绘制整行嘴巴弧线
                rect(mouthX, mouthY, mouthWidth, 1, true);
            }
            setBrushColor(1);
            break;
            
        case 2: // SMILE - 微笑
            // 使用更柔和的圆形脸
            drawCircle(centerX, centerY, width/2 * 0.9, true);
            
            // 绘制温和的眼睛（半圆形，显得更友好）
            setBrushColor(0);
            // 左眼 - 上半圆
            for(int scanY = 0; scanY < height/25; scanY++) {
                float eyeRadius = width/20;
                float offset = sqrt(eyeRadius*eyeRadius - scanY*scanY);
                int eyeX = x + width/3 - offset;
                int eyeY = y + height/4 - scanY/2;
                rect(eyeX, eyeY, offset*2, 1, true);
            }
            // 右眼 - 上半圆
            for(int scanY = 0; scanY < height/25; scanY++) {
                float eyeRadius = width/20;
                float offset = sqrt(eyeRadius*eyeRadius - scanY*scanY);
                int eyeX = x + width*2/3 - offset;
                int eyeY = y + height/4 - scanY/2;
                rect(eyeX, eyeY, offset*2, 1, true);
            }
            
            // 绘制温和的微笑嘴巴（更微妙的弧度）
            for(int scanY = 0; scanY < height/10; scanY++) {
                float normalizedY = (float)scanY / (height/10);
                // 使用更平缓的曲线
                float mouthWidth = width/2 * (0.8 + 0.2 * cos(normalizedY * PI));
                int mouthX = x + width/2 - mouthWidth/2;
                int mouthY = y + height*3/5 - scanY * 0.8; // 更温和的上扬
                
                // 绘制整行嘴巴弧线
                rect(mouthX, mouthY, mouthWidth, 1, true);
            }
            setBrushColor(1);
            break;
            
        case 3: // SAD - 悲伤
            // 使用稍微拉长的脸形，显得更沮丧
            for(int scanY = 0; scanY < height*0.9; scanY++) {
                float faceWidth = width*0.85 * (0.9 + 0.1 * cos(scanY * PI / (height*0.9)));
                int faceX = x + width/2 - faceWidth/2;
                int faceY = y + (height - height*0.9)/2 + scanY;
                rect(faceX, faceY, faceWidth, 1, true);
            }
            
            // 绘制悲伤的眼睛（下垂的泪滴形状）
            setBrushColor(0);
            // 左眼 - 泪滴形
            for(int scanY = 0; scanY < height/15; scanY++) {
                float eyeWidth = width/12 * (1 + scanY * 0.5 / (height/15));
                int eyeX = x + width/3 - eyeWidth/2;
                int eyeY = y + height/4 + scanY;
                rect(eyeX, eyeY, eyeWidth, 1, true);
            }
            // 右眼 - 泪滴形
            for(int scanY = 0; scanY < height/15; scanY++) {
                float eyeWidth = width/12 * (1 + scanY * 0.5 / (height/15));
                int eyeX = x + width*2/3 - eyeWidth/2;
                int eyeY = y + height/4 + scanY;
                rect(eyeX, eyeY, eyeWidth, 1, true);
            }
            
            // 添加一滴眼泪效果
            for(int scanY = 0; scanY < height/10; scanY++) {
                float tearWidth = width/30 * (1 - scanY * 0.4 / (height/10));
                int tearX = x + width/3 - tearWidth/2;
                int tearY = y + height/3 + scanY;
                rect(tearX, tearY, tearWidth, 1, true);
            }
            
            // 绘制悲伤的嘴巴（明显向下弯曲的弧形）
            for(int scanY = 0; scanY < height/4; scanY++) {
                float normalizedY = (float)scanY / (height/4);
                float mouthWidth = width*2/3 * (0.7 + 0.3 * cos(normalizedY * PI));
                int mouthX = x + width/2 - mouthWidth/2;
                int mouthY = y + height*3/5 + scanY * 1.2; // 更明显的下弯
                
                // 绘制整行嘴巴弧线
                rect(mouthX, mouthY, mouthWidth, 1, true);
            }
            setBrushColor(1);
            break;
            
        case 4: // CONFUSED - 困惑
            // 使用圆形脸
            drawCircle(centerX, centerY, width/2 * 0.9, true);
            
            // 绘制困惑的眼睛（不对称，带问号效果）
            setBrushColor(0);
            // 左眼（圆睁）- 使用圆形
            drawCircle(x + width/3, y + height/4, width/12, true);
            // 右眼（疑惑）- 椭圆形，向下倾斜
            for(int scanY = 0; scanY < height/20; scanY++) {
                float eyeWidth = width/14 * (1 + scanY * 0.3 / (height/20));
                int eyeX = x + width*2/3 - eyeWidth/2 + scanY/3;
                int eyeY = y + height*2/5 + scanY;
                rect(eyeX, eyeY, eyeWidth, 1, true);
            }
            
            // 添加问号形状的眉毛
            for(int scanY = 0; scanY < height/25; scanY++) {
                float browWidth = width/10 * (0.5 + 0.5 * sin(scanY * PI / (height/25)));
                int browX = x + width*3/5 - browWidth;
                int browY = y + height/6 + scanY;
                rect(browX, browY, browWidth, 1, true);
            }
            
            // 绘制困惑的嘴巴（水平波浪线）
            for(int scanY = 0; scanY < height/20; scanY++) {
                float offset = sin(scanY * PI * 2 / (height/20)) * width/12;
                int mouthX = x + width/3 - offset/2;
                int mouthY = y + height*2/3 + scanY;
                
                // 绘制困惑的嘴巴
                rect(mouthX, mouthY, width/3 + offset, 1, true);
            }
            setBrushColor(1);
            break;
            
        case 5: // ANGRY - 生气
            // 使用稍微扭曲的脸形，显得更愤怒
            for(int scanY = 0; scanY < height; scanY++) {
                float faceWidth = width*0.9 * (1.0 + 0.05 * sin(scanY * PI / height));
                int faceX = x + width/2 - faceWidth/2;
                int faceY = y + scanY;
                rect(faceX, faceY, faceWidth, 1, true);
            }
            
            // 绘制愤怒的眼睛（锐利的三角形）
            setBrushColor(0);
            // 左眼 - 锐利的三角形
            for(int scanY = 0; scanY < height/15; scanY++) {
                float eyeWidth = width/10 * (1 - scanY * 0.6 / (height/15));
                int eyeX = x + width/4 - scanY*2;
                int eyeY = y + height/4 + scanY;
                rect(eyeX, eyeY, eyeWidth, 1, true);
            }
            // 右眼 - 锐利的三角形
            for(int scanY = 0; scanY < height/15; scanY++) {
                float eyeWidth = width/10 * (1 - scanY * 0.6 / (height/15));
                int eyeX = x + width*3/4 - width/10 + scanY*2;
                int eyeY = y + height/4 + scanY;
                rect(eyeX, eyeY, eyeWidth, 1, true);
            }
            
            // 绘制愤怒的眉毛（倒立的V形）
            for(int scanY = 0; scanY < height/20; scanY++) {
                int browLength = width/6 * (1 - scanY * 0.7 / (height/20));
                // 左眉毛 - 向上倾斜
                rect(x + width/5 - scanY*1.5, y + height/8 - scanY/2, browLength, 1, true);
                // 右眉毛 - 向上倾斜
                rect(x + width*2/3 - browLength + scanY*1.5, y + height/8 - scanY/2, browLength, 1, true);
            }
            
            // 绘制愤怒的嘴巴（向下弯曲的尖角形）
            for(int scanY = 0; scanY < height/5; scanY++) {
                float normalizedY = (float)scanY / (height/5);
                float mouthWidth = width/2 * (0.8 - 0.3 * cos(normalizedY * PI));
                int mouthX = x + width/2 - mouthWidth/2;
                int mouthY = y + height*3/5 + scanY * 1.5;
                
                // 绘制整行嘴巴弧线
                rect(mouthX, mouthY, mouthWidth, 1, true);
            }
            setBrushColor(1);
            break;
            
        case 6: // ASLEEP - 睡觉
            // 使用平静的圆形脸
            drawCircle(centerX, centerY, width/2 * 0.9, true);
            
            // 绘制闭着的眼睛（Z字形线条）
            setBrushColor(0);
            // 左眼 - Z字形
            for(int i = 0; i < 3; i++) {
                // 第一笔画
                for(int scanY = 0; scanY < height/40; scanY++) {
                    int eyeX = x + width/5 + scanY*3;
                    int eyeY = y + height/4 + scanY;
                    rect(eyeX, eyeY, width/15 - scanY*2, 1, true);
                }
                // 第二笔画
                for(int scanY = 0; scanY < height/40; scanY++) {
                    int eyeX = x + width/5 + width/15 - scanY*3;
                    int eyeY = y + height/4 + height/40 + scanY;
                    rect(eyeX, eyeY, width/15 - scanY*2, 1, true);
                }
            }
            // 右眼 - Z字形
            for(int i = 0; i < 3; i++) {
                // 第一笔画
                for(int scanY = 0; scanY < height/40; scanY++) {
                    int eyeX = x + width*2/3 + scanY*3;
                    int eyeY = y + height/4 + scanY;
                    rect(eyeX, eyeY, width/15 - scanY*2, 1, true);
                }
                // 第二笔画
                for(int scanY = 0; scanY < height/40; scanY++) {
                    int eyeX = x + width*2/3 + width/15 - scanY*3;
                    int eyeY = y + height/4 + height/40 + scanY;
                    rect(eyeX, eyeY, width/15 - scanY*2, 1, true);
                }
            }
            
            // 绘制平静的嘴巴（轻微上扬的直线）
            rect(x + width/3, y + height*2/3, width/3, height/20, true);
            setBrushColor(1);
            break;
            
        case 7: // SURPRISED - 惊讶
            // 使用圆形脸
            drawCircle(centerX, centerY, width/2 * 0.9, true);
            
            // 绘制惊讶的眼睛（圆睁，更大，带明显高光）
            setBrushColor(0);
            // 左眼 - 大圆形
            drawCircle(x + width/3, y + height/4, width/10, true);
            // 右眼 - 大圆形
            drawCircle(x + width*2/3, y + height/4, width/10, true);
            // 添加明显的白色高光
            setBrushColor(1);
            drawCircle(x + width/3 - width/20, y + height/4 - width/20, width/20, true);
            drawCircle(x + width*2/3 - width/20, y + height/4 - width/20, width/20, true);
            setBrushColor(0);
            
            // 添加扬起的眉毛，表示惊讶
            for(int scanY = 0; scanY < height/30; scanY++) {
                int browLength = width/8 * (1 + scanY * 0.5 / (height/30));
                // 左眉毛
                rect(x + width/5, y + height/8 - scanY*2, browLength, 1, true);
                // 右眉毛
                rect(x + width*2/3 - browLength, y + height/8 - scanY*2, browLength, 1, true);
            }
            
            // 绘制惊讶的嘴巴（O形，更大更圆）
            for(int scanY = 0; scanY < height/3; scanY++) {
                float normalizedY = (float)scanY / (height/3);
                float normalizedRadius = 0.7;
                float offset = sqrt(normalizedRadius * normalizedRadius - (normalizedY - normalizedRadius) * (normalizedY - normalizedRadius));
                
                int mouthWidth = offset * width*0.9;
                int mouthX = x + width/2 - mouthWidth/2;
                int mouthY = y + height*2/3 + scanY;
                
                // 绘制整行嘴巴弧线
                rect(mouthX, mouthY, mouthWidth, 1, true);
            }
            setBrushColor(1);
            break;
            
        case 8: // SILLY - 愚蠢
            // 使用稍微倾斜的脸形，增加幽默感
            for(int scanY = 0; scanY < height; scanY++) {
                float tilt = sin(scanY * PI / height) * width/10;
                float faceWidth = width*0.85;
                int faceX = x + width/2 - faceWidth/2 + tilt;
                rect(faceX, y + scanY, faceWidth, 1, true);
            }
            
            // 绘制愚蠢的眼睛（不对称，一个向上看，一个向下看）
            setBrushColor(0);
            // 左眼（大，向上）- 椭圆形
            for(int scanY = 0; scanY < height/18; scanY++) {
                float eyeWidth = width/10 * (1 - scanY * 0.4 / (height/18));
                int eyeX = x + width/3 - width/20 - scanY/3;
                int eyeY = y + height/5 + scanY;
                rect(eyeX, eyeY, eyeWidth, 1, true);
            }
            // 右眼（小，向下）- 椭圆形
            for(int scanY = 0; scanY < height/22; scanY++) {
                float eyeWidth = width/16 * (1 + scanY * 0.5 / (height/22));
                int eyeX = x + width*3/5 + width/20 + scanY/2;
                int eyeY = y + height*2/5 + scanY;
                rect(eyeX, eyeY, eyeWidth, 1, true);
            }
            
            // 添加一个翘起的眉毛
            for(int scanY = 0; scanY < height/30; scanY++) {
                int browLength = width/10 * (1 + scanY * 0.6 / (height/30));
                rect(x + width/3 - browLength/2, y + height/8 - scanY*2, browLength, 1, true);
            }
            
            // 绘制愚蠢的嘴巴（歪扭的大微笑）
            for(int scanY = 0; scanY < height/5; scanY++) {
                float normalizedY = (float)scanY / (height/5);
                float mouthWidth = width/2 * (0.8 + 0.2 * sin(normalizedY * PI * 2));
                float asymmetry = sin(normalizedY * PI) * width/8;
                int mouthX = x + width/3 - asymmetry/2;
                int mouthY = y + height*2/3 - normalizedY * height/7;
                
                // 绘制歪扭的嘴巴
                rect(mouthX, mouthY, mouthWidth + asymmetry, 1, true);
            }
            setBrushColor(1);
            break;
            
        case 9: // ROCK - 石头手势
            // 优化石头手势绘制，使用矩形区域绘制
            // 手掌 - 一次性绘制矩形区域
            rect(x + width/4, y + height/5, width/2, height*4/5, true);
            // 拇指 - 一次性绘制矩形区域
            rect(x, y, width/4, height*3/5, true);
            // 食指 - 一次性绘制矩形区域
            rect(x + width*3/4, y, width/4, height*3/5, true);
            break;
            
        case 10: // SCISSORS - 剪刀手势
            // 优化剪刀手势绘制，使用矩形区域绘制
            // 中指 - 一次性绘制矩形区域
            rect(x + width/3, y, width/8, height, true);
            // 食指（倾斜）- 优化为使用HLineOnePixel减少I2C传输
            for(int scanY = y + height/4; scanY < y + height; scanY++) {
                int offset = (scanY - (y + height/4)) / 4;
                HLineOnePixel(x + width*2/3 - offset, scanY, width/8);
            }
            break;
            
        case 11: // PAPER - 布手势
            // 优化布手势绘制，使用矩形区域和线段绘制
            // 手掌 - 一次性绘制矩形区域
            rect(x, y + height/3, width, height*2/3, true);
            
            // 拇指 - 使用斜线绘制
            for(int scanY = y + height/5; scanY < y + height/2; scanY++) {
                int offset = (scanY - y - height/5) * 2;
                if(offset < width/4) {
                    HLineOnePixel(x, scanY, width/5 + offset);
                }
            }
            
            // 食指 - 使用斜线绘制
            for(int scanY = y; scanY < y + height/3; scanY++) {
                int offset = (y + height/3 - scanY) * 2;
                if(offset < width/3) {
                    HLineOnePixel(x + width/5, scanY, offset + width/20);
                }
            }
            
            // 中指 - 一次性绘制矩形区域
            rect(x + width/2 - width/20, y, width/10, height/3, true);
            
            // 无名指 - 使用斜线绘制
            for(int scanY = y; scanY < y + height/3; scanY++) {
                int offset = (y + height/3 - scanY) * 2;
                if(offset < width/3) {
                    HLineOnePixel(x + width*3/5 - offset, scanY, width*2/3 - (x + width*3/5 - offset) + x);
                }
            }
            
            // 小指 - 使用斜线绘制
            for(int scanY = y; scanY < y + height/3; scanY++) {
                int offset = (y + height/3 - scanY) * 2;
                if(offset < width/4) {
                    HLineOnePixel(x + width*3/4 - offset, scanY, x + width - (x + width*3/4 - offset));
                }
            }
            break;
            
        default: // 默认显示心形
            // 使用改进的心形绘制算法
            for(int pixelY = 0; pixelY < height; pixelY++) {
                for(int pixelX = 0; pixelX < width; pixelX++) {
                    // 计算归一化坐标
                    float normalizedX = (float)(pixelX - width/2) / (width/2);
                    float normalizedY = (float)(pixelY - height/2) / (height/2) * 1.3;
                    
                    // 使用更精确的心形公式
                    float x2 = normalizedX * normalizedX;
                    float y2 = normalizedY * normalizedY;
                    float term1 = pow(x2 + y2 - 0.95f, 3.0f);
                    float term2 = 0.8f * x2 * pow(normalizedY, 3.0f);
                    
                    // 使用阈值判断
                    if(term1 - term2 <= 0.05f) {
                        // 直接使用rect绘制单个像素点
                        rect(x + pixelX, y + pixelY, 1, 1, true);
                    }
                }
            }
            break;
    }
    
    // 刷新区域
    regionalRefresh(x, y, width, height);
    // 恢复画笔颜色
    setBrushColor(_brushColor);
}

void OLED_12864::rect(int32_t x, int32_t y, int32_t rectWidth, int32_t rectHeight, bool fill)
{
    // 确保坐标在有效范围内并裁剪
    if (x < 0) x = 0;
    if (y < 0) y = 0;
    if (x > 127) x = 127;
    if (y > 63) y = 63;
    
    // 处理负数尺寸
    if(rectWidth < 0) {
        x += rectWidth;
        rectWidth = -rectWidth;
    }
    if(rectHeight < 0) {
        y += rectHeight;
        rectHeight = -rectHeight;
    }
    
    // 检查尺寸有效性
    if(rectWidth == 0 || rectHeight == 0 || x > 127 || y > 63) {
        return;
    }
    
    // 裁剪到屏幕范围内
    if(x + rectWidth > 128) rectWidth = 128 - x;
    if(y + rectHeight > 64) rectHeight = 64 - y;
    
    if(fill) {
        // 实心填充：对于每个像素点，直接使用HLineOnePixel进行行填充
        // 这种方式更简单直接，确保每个像素都被正确填充
        for(int i = 0; i < rectHeight; i++) {
            // 对于每一行，直接调用HLineOnePixel填充整行
            HLineOnePixel(x, y + i, rectWidth);
        }
    }else{
        // 绘制矩形边框
        // 顶部边框
        for(int i=0; i<brushWidth; i++) 
            HLineOnePixel(x, y+i, rectWidth);
        // 底部边框
        for(int i=0; i<brushWidth; i++) 
            HLineOnePixel(x, y+rectHeight-brushWidth+i, rectWidth);
        // 左边框
        for(int i=0; i<brushWidth; i++) 
            VLineOnePixel(x+i, y, rectHeight);
        // 右边框
        for(int i=0; i<brushWidth; i++) 
            VLineOnePixel(x+rectWidth-brushWidth+i, y, rectHeight);
    }
}

